package songZhao.MinaDemo.IObuffer;

import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;

import org.apache.mina.core.buffer.IoBuffer;

import songZhao.MinaDemo.Example.ProtocolPack;

public class TestPack {
	public static void main(String[] args) throws CharacterCodingException {
		String content = "报文内容";
		ProtocolPack pack = new ProtocolPack((byte) 111, content);
		System.out.println(pack);
		IoBuffer buffer = IoBuffer.allocate(1024);
		buffer.setAutoExpand(true);
		
		
		buffer.put(pack.getFlag());
		buffer.put(pack.getContent().getBytes(Charset.forName("utf-8")));
		buffer.putInt(pack.getLength());
		/**
		 * 获取报文的内容,以长度坐标为准.主要取玩字符串之后,重置下position
		 */
		buffer.flip();
		System.out.println(buffer.get());
		System.out.println(buffer.getString(Charset.forName("utf-8").newDecoder()));
		buffer.position(content.getBytes(Charset.forName("utf-8")).length+1);
		System.out.println(buffer.getInt());
	}
}
